package com.bicon.botu.mall.order.service.impl;

/**
 * 发票
 *
 * @author 李双喜
 * @date 2018/5/14 12:57
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.entity.MyInvoice;
import com.bicon.botu.mall.order.entity.OrderGoods;
import com.bicon.botu.mall.order.mapper.MyInvoiceMapper;
import com.bicon.botu.mall.order.service.IMyInvoiceService;
import com.bicon.botu.mall.order.service.IOrderGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class MyInvoiceServiceImpl extends ServiceImpl<MyInvoiceMapper, MyInvoice> implements IMyInvoiceService {

    @Autowired
    private MyInvoiceMapper myInvoiceMapper;

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    /**
     * 新增或修改个人发票信息
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public boolean saveOrUpdate(MyInvoice entity,String orderGoodsId){
        boolean result=false;
        //根据id（用户ID) 查询是否已存在记录
        int count=myInvoiceMapper.selectCount(new EntityWrapper<MyInvoice>().eq("id", entity.getId())
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        //重置当前用户下所有发票记录标识
        //根据数据库记录，执行新增或修改
        if(count==0){
            entity.preInsert();
            result=insert(entity);
            if(result){
                OrderGoods orderGoods=new OrderGoods();
                orderGoods.setId(orderGoodsId);
                orderGoods.setInvoiceId(entity.getId());
                result=iOrderGoodsService.updateById(orderGoods);
            }
        }else{
            entity.preUpdate();
            result=updateAllColumnById(entity);
        }
        return result;
    }
}
